*Change working directory
cd "/Users/gehlbach/Dropbox/Projects/Handbook paper/Data"
clear matrix
clear
set mem 3000m
set matsize 2000


/* LOAD DATA */

*full4.dta:
  *linearly interpolates capital stock from adjacent years for any firm missing that variable in 1996, 1997, or 2002, regardless of region
  *linearly interpolates employment from adjacent years for firms in region 1163 in 2000
  *interpolates capital stock for all firms in 2005 as capital stock in 2004

use full4, clear

*for aggregate estimates:
  *drop obs in 2005

drop if year == 105

*drop outliers

gen cap = exp(logcap)
gen emp = exp(logemp)
gen out = exp(lsale)
*
foreach x of varlist cap emp out {
* 
by okpo, sort: gen last=1 if `x'~=. & f.`x'==.
by okpo, sort: gen first=1 if `x'~=. & l.`x'==.
* 
by okpo: gen out1=1 if `x'/l.`x'>5 & f.`x'/`x'<0.2 & `x'~=. & l.`x'~=. & f.`x'~=.
by okpo: gen out2=1 if `x'/l.`x'<0.2 & f.`x'/`x'>5 & `x'~=. & l.`x'~=. & f.`x'~=.
by okpo: gen out3=1 if f.`x'/`x'>10 & `x'~=. & f.`x'~=. & first==1 & f.out1~=1 & f.out2~=1 &  f2.`x'~=.
by okpo: gen out4=1 if f.`x'/`x'<0.1 & `x'~=. & f.`x'~=. & first==1 & f.out1~=1 & f.out2~=1 &  f2.`x'~=.
by okpo: gen out5=1 if `x'/l.`x'>10 & `x'~=. & l.`x'~=. & last==1 & l.out1~=1 & l.out2~=1 &  l2.`x'~=.
by okpo: gen out6=1 if `x'/l.`x'<0.1 & `x'~=. & l.`x'~=. & last==1 & l.out1~=1 & l.out2~=1 &  l2.`x'~=.
* 
gen outlier`x'=1 if out1==1 | out2==1 | out3==1 | out4==1 | out5==1 |out6==1
drop out1 out2 out3 out4 out5 out6
drop last first
}
*
drop if outliercap == 1
drop if outlieremp == 1
drop if outlierout == 1

*drop missing values (do this after dropping outliers so that can take advantage of time series where exists)

drop if missing(year,ter,lsale,logemp,logcap,ind,ldo,lfo,lho,lmo)


/* VARIABLE CONSTRUCTION BEFORE DETRENDING */

*year dummies
tab year, gen(y)
local j=1
forval i=1/20 {
local j=`i'+84
rename y`i' y`j'
label variable y`j' `""'
}

*industry-year dummies
foreach i of numlist 85/104 {
forv j = 1/10 {
gen ind`j'_y`i' = ind`j'*y`i'
}
}

save priv_aggregate, replace


/* DETRENDING */

tsset okpo year
DETREND lsale logemp logcap ldo lho lmo lfo e1-e10 c1-c10 y85-y104, by(okpo) time(year)
rename _DTlsale dtlsale
rename _DTlogcap dtlogcap
rename _DTlogemp dtlogemp
rename _DTldo dtldo
rename _DTlho dtlho
rename _DTlmo dtlmo
rename _DTlfo dtlfo
forv i = 1/10 {
rename _DTe`i' dte`i'
}
forv i = 1/10 {
rename _DTc`i' dtc`i'
}
foreach i of numlist 85/104 {
rename _DTy`i' dty`i'
}

save dtwork_aggregate, replace


/* VARIABLE CONSTRUCTION AFTER DETRENDING */

*dt_industry*year interactions
foreach i of numlist 85/104 {
forv j = 1/10 {
gen dtind`j'_y`i' = ind`j'*dty`i'
}
}

save interwork_aggregate, replace 


/* DATA DESCRIPTION */

collapse ldo lfo, by(year)
scatter ldo year, c(l) || scatter lfo year, c(l)

/* AVERAGE PRIVATIZATION EFFECT */ 

use interwork_aggregate, clear

*OLS
reg lsale ldo lfo e1-e10 c1-c10 ind1_y85-ind10_y104, cluster(okpo)
*FE
tsset okpo year
xtreg lsale ldo lfo e1-e10 c1-c10 ind1_y85-ind10_y104, cluster(okpo) fe
*FEFT
reg dtlsale dtldo dtlfo dte* dtc* dtind*_y*, cluster(okpo)

*separate out mixed and 100% private
*OLS
reg lsale lho lmo lfo e1-e10 c1-c10 ind1_y85-ind10_y104, cluster(okpo)
*FE
xtreg lsale lho lmo lfo e1-e10 c1-c10 ind1_y85-ind10_y104, cluster(okpo) fe
*FEFT
reg dtlsale dtlho dtlmo dtlfo dte* dtc* dtind*_y*, cluster(okpo)
